﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 多态
{
    class Program
    {
        /// <summary>
        /// 多态的例子，知识点：关键词is as 
        /// is : A is B用来判断A是否是B类型
        /// as : A as B 强制向下转化，如果转化不成功返回null，成功则返回成功转化后的对象
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            Employee[] employees = new Employee[4];
            SalariedEmployee employee1 = new SalariedEmployee("smith", "wood", "ssn", 330M);
            HourlyEmployee employee2 = new HourlyEmployee("lee", "kafu", "ssn", 18M, 10M);
            CommissionEmployee employee3 = new CommissionEmployee("lee", "bruz", "ssn", 0.2M, 2000M);
            BasePlusCommissionEmployee employee4 = new BasePlusCommissionEmployee("qiao", "buz", "ssn", 0.02M, 2000M, 200);

            employees[0] = employee1;
            employees[1] = employee2;
            employees[2] = employee3;
            employees[3] = employee4;

            Console.WriteLine("Employees processed individually:\n");

            Console.WriteLine("{0}\nearned: {1:C}\n", employee1.ToString(), employee1.Earning());
            Console.WriteLine("{0}\nearned: {1:C}\n", employee2.ToString(), employee2.Earning());
            Console.WriteLine("{0}\nearned: {1:C}\n", employee3.ToString(), employee3.Earning());
            Console.WriteLine("{0}\nearned: {1:C}\n", employee4.ToString(), employee4.Earning());

            Console.WriteLine("Employees processed polymorphically:\n");

            
            foreach (var currentEmployee in employees)
            {
                Console.WriteLine(currentEmployee.ToString());

                #region   is 方式判断多态的类型
            //    if (currentEmployee is BasePlusCommissionEmployee)
            //    {
            //        //必须强制转化为BasePlusCommissionEmployee才可以用她当中特有的属性
            //        BasePlusCommissionEmployee employee = (BasePlusCommissionEmployee)currentEmployee;

            //        employee.BaseSalary *= 1.1M;
            //        Console.WriteLine("new base salary with 10% increase is : {0}", employee.BaseSalary);
            //    }//end if
                 #endregion

                #region   as 进行向下强制类型转化

                var employee = currentEmployee as BasePlusCommissionEmployee;

                if (employee != null)//转化成功，确实是BasePlusCommissionEmployee
                {
                    employee.BaseSalary *= 1.1M;
                    Console.WriteLine("new base salary with 10% increase is : {0}", employee.BaseSalary);
                }

                #endregion 

                Console.WriteLine("earned {0:C}\n", currentEmployee.Earning());
            }//end for

            for(int i = 0 ; i< employees.Length;i++)
            {
                Console.WriteLine("Employee {0} is a {1}", i, employees[i].GetType());
            }
            Console.ReadKey();

        }//end method main
    }
}
